package com.run.motion.controller;

import java.util.ArrayList;
import java.util.List;

import com.run.motion.config.IpConfig;
import com.run.motion.entity.Gym;
import com.run.motion.entity.GymEquipment;
import com.run.motion.entity.GymImages;
import com.run.motion.service.IGymEquipmentService;
import com.run.motion.service.IGymImagesService;
import com.run.motion.service.IGymService;
import com.run.motion.util.page.AjaxResult;
import com.run.motion.util.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 健身房Controller
 * 
 * @author lyq
 * @date 2022-03-26
 */
@Api(tags = "健身房")
@RestController
@RequestMapping("/gym")
public class GymController extends BaseController
{
    @Autowired
    private IGymService gymService;

    @Autowired
    private IGymEquipmentService iGymEquipmentService;

    @Autowired
    private IGymImagesService iGymImagesService;

    @Autowired
    private IpConfig ipConfig;

    /**
     * 查询健身房列表
     */
    @ApiOperation(value = "查询健身房列表")
    @GetMapping("/list")
    public TableDataInfo list(Gym gym)
    {
        startPage(); // 设置分页
        List<Gym> gyms = gymService.selectGymList(gym); // 获取健身房数据
        for (int i = 0; i < gyms.size(); i++) {
            gyms.get(i).setImg(ipConfig.getIp()+gyms.get(i).getImg()); //拼接图片地址
        }
        return getDataTable(gyms);
    }


    /**
     * 获取健身房详细信息
     */
    @ApiOperation(value = "获取健身房详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Gym gym =  gymService.selectGymById(id);
        gym.setImg(ipConfig.getIp()+gym.getImg());
        List<GymEquipment> equipments = iGymEquipmentService.selectGymEquipmentList(); // 获取全部健身器材数据
        GymImages gymImage = new GymImages();
        gymImage.setGymId(id);
        List<GymImages> gymImages = iGymImagesService.selectGymImagesList(gymImage); // 获取轮播图片
        for (int i = 0; i < gymImages.size(); i++) {
            gymImages.get(i).setUrl(ipConfig.getIp()+gymImages.get(i).getUrl()); // 图片拼接
        }
        if (!gym.getEqIds().isEmpty()){
            String[] strs = gym.getEqIds().split(","); // 分割运动器材id
            List<GymEquipment> addEQ = new ArrayList<>();
            for (String str: strs) {
                for (int j = 0; j < equipments.size(); j++) {
                    if (equipments.get(j).getId().toString().equals(str)){ // 如果健身房有该器材id，那么添加数据到该健身房
                        equipments.get(j).setUrl(ipConfig.getIp()+equipments.get(j).getUrl()); //拼接图片地址
                        addEQ.add(equipments.get(j));
                    }
                }
            }
            gym.setGymImages(gymImages);
            gym.setGymEquipments(addEQ); // 添加数据
        }
        return AjaxResult.success(gym);
    }

    /**
     * 新增健身房
     */
    @ApiOperation(value = "新增健身房")
    @PostMapping
    public AjaxResult add(@RequestBody Gym gym)
    {
        return toAjax(gymService.insertGym(gym));
    }

    /**
     * 修改健身房
     */
    @ApiOperation(value = "修改健身房")
    @PutMapping
    public AjaxResult edit(@RequestBody Gym gym)
    {
        return toAjax(gymService.updateGym(gym));
    }

    /**
     * 删除健身房
     */
    @ApiOperation(value = "删除健身房")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gymService.deleteGymByIds(ids));
    }
}
